<?php

namespace App\Http\Controllers\Admin\v1;

use App\Http\Controllers\Controller;
use App\Models\ApiDocument;
use App\Models\ApiGateway;
use App\Services\Utils\Utils;
use Illuminate\Http\Request;
use App\Http\Controllers\ApiController;
use App\Services\Utils\Guard;

class ApiDocumentController extends ApiController
{
    public function index(Request $request)
    {
        $priceList = Utils::getApiPriceList();

        $apiDocs = ApiDocument::where(['group' => $request->group, 'display' => 1])
                                ->select('group', 'name', 'title', 'descript')
                                ->get();
        $this->mappingApiDocs($apiDocs, $priceList);
        return $this->response(compact('apiDocs'));
    }

    public function mappingApiDocs($docs, $prices)
    {
        foreach ($docs as $item) {
            $group = $item->group;
            $name = $item->name;
            $price = $prices->$group->$name->price;
            $item['price'] = $price/100.0;
        }
    }

    public function detail(Request $request)
    {
        $detail = ApiDocument::where(['group' => $request->group, 'name' => $request->name])
                               ->select('detail')
                               ->get();
        return $this->response(compact('detail'));
    }

    //todo 用来直接返回所有api的价格
    // public function apiPrices()
    // {
    //     $apiDocs = ApiDocument::where('display', 1)
    //                         ->select('group', 'name', 'title')
    //                         ->get();
    //     $priceList = Utils::getApiPriceList();
    //     $this->mappingApiPrices($apiDocs, $priceList, $request->apikey_id, $request->type);
    //     return $this->response(compact('apiDocs'));
    // }

    // public function mappingApiPrices($docs, $prices, $apikey_id, $type)
    // {
    //     foreach ($docs as $item) {
    //         $group = $item->group;
    //         $name = $item->name;
    //         $price = $prices->$group->$name->price;
    //         $apigateway = ApiGateway::where([
    //                                     'apikey_id' => $apikey_id,
    //                                     'group' => $group,
    //                                     'endpoint' => $name,
    //                                 ])->first();
    //         if (isset($apigateway) || $type == 0) {
    //             $item['price'] = '免费';
    //         } else {
    //             $item['price'] = $price/100.0.'元/次';
    //         }
    //     }
    // }
}
